package com.app.util.validate;

import com.app.util.type.DoubleUtil;
import com.app.util.type.FloatUtil;

class Condition {

	
	static boolean gt(Double value, String formula) {
		String num = formula.replaceAll(">", "").trim();
		if (!FloatUtil.isFloat(num))
			return false;
		if (value > DoubleUtil.parseDouble(num))
			return true;
		return false;
	}

	static boolean lt(Double value, String formula) {
		String num = formula.replaceAll("<", "").trim();
		if (!FloatUtil.isFloat(num))
			return false;
		if (value < DoubleUtil.parseDouble(num))
			return true;
		return false;
	}

	static boolean gteq(Double value, String formula) {
		String num = formula.replaceAll(">=", "").trim();
		if (!FloatUtil.isFloat(num))
			return false;
		if (value >= DoubleUtil.parseDouble(num))
			return true;
		return false;
	}

	static boolean lteq(Double value, String formula) {
		String num = formula.replaceAll("<=", "").trim();
		if (!FloatUtil.isFloat(num))
			return false;
		if (value <= DoubleUtil.parseDouble(num))
			return true;
		return false;
	}

	static boolean eq(Double value, String formula) {
		String num = formula.replaceAll("=", "").trim();
		if (!FloatUtil.isFloat(num))
			return false;
		if (value == DoubleUtil.parseDouble(num))
			return true;
		return false;
	}

	static boolean noteq(Double value, String formula) {
		String num = formula.replaceAll("!=", "").trim();
		if (!FloatUtil.isFloat(num))
			return false;
		if (value != DoubleUtil.parseDouble(num))
			return true;
		return false;
	}

	static boolean area(Double value, String formula) {
		boolean lx = formula.indexOf("(") != -1;
		boolean rx = formula.indexOf(")") != -1;
		boolean lz = formula.indexOf("[") != -1;
		boolean rz = formula.indexOf("]") != -1;

		String numstr = formula.replaceAll("[@\\(\\)\\[\\]]", "").trim();
		String[] nums = numstr.split(",");
		nums[0] = nums[0].trim();
		nums[1] = nums[1].trim();
		if (nums.length != 2)
			return false;
		if (!FloatUtil.isFloat(nums[0]))
			return false;
		if (!FloatUtil.isFloat(nums[1]))
			return false;

		boolean ok = true;
		if (lx)
			ok &= value > DoubleUtil.parseDouble(nums[0]);
		else if (lz)
			ok &= value >= DoubleUtil.parseDouble(nums[0]);
		else
			return false;

		if (rx)
			ok &= DoubleUtil.parseDouble(nums[1]) > value;
		else if (rz)
			ok &= DoubleUtil.parseDouble(nums[1]) >= value;
		else
			return false;

		return ok;
	}
}
